class Solution:
    def countCompleteSubstrings(self, word: str, k: int) -> int:
        n = len(word)
        s = [[0 for _ in range(26)] for _ in range(n + 1)]
        last_index = 0
        ans = 0
        for index, c in enumerate(word):
            ord_c = ord(c) - ord('a')
            for i in range(26):
                if i == ord_c:
                    s[index + 1][i] = s[index][i] + 1
                else:
                    s[index + 1][i] = s[index][i]
            if index > 0 and abs(ord(word[index]) - ord(word[index - 1])) > 2:
                last_index = index
            for j in range(1, 27):
                last = index + 1 - j * k
                if last >= last_index: 
                    flag = 0
                    for t in range(26):
                        if s[index + 1][t] - s[last][t] == 0 or s[index + 1][t] - s[last][t] == k:
                            continue
                        else:
                            if s[index + 1][t] - s[last][t] > k:
                                flag = 2
                                break
                            else:
                                flag = 1
                                break
                    if flag == 0:
                        ans += 1
                    if flag == 2:
                        break           
                else:
                    break
            print(index, ans)
        return ans 

data = Solution()
word = "hmfttfmhpttmpphmhthfpffphftfhthtmhthhfphfmmm"
k = 1
print(data.countCompleteSubstrings(word, k))